<template>
  <d2-container class="page">
    <el-dialog
      width="800px"
      :title="createOrEdit"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form" ref="form">
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            action="https://xcx.ruiysoft.com/reserve/qiniu/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :limit="1"
            :on-success="handleUploadSuccess"
            :file-list="fileList"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.catalogId"
            filterable
            placeholder="请选择"
            @change="onSelectedChange"
          >
            <el-option
              v-for="item in catalogList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品" :label-width="formLabelWidth">
          <el-select v-model="form.linkUrl" filterable placeholder="请选择">
            <el-option
              v-for="item in skuList"
              :key="item.skuId"
              :label="item.skuName"
              :value="item.skuId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCreateNew('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-button type="primary" size="mini" @click="showCreateDlg('form')">新建</el-button>
    <el-button type="primary" size="mini" @click="getList()">刷新</el-button>
    <el-divider></el-divider>
    <el-table
      stripe
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <!-- <el-table-column label="id" width="120">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>-->
      <el-table-column label="图片">
        <template slot-scope="scope" width="100">
          <img v-bind:src="scope.row.imgUrl" style="width:50%" />
        </template>
      </el-table-column>
      <el-table-column label="链接商品" width="120">
        <template slot-scope="scope">{{ scope.row.goodsName }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditDlg(scope.row)" icon="el-icon-edit"></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination background layout="prev, pager, next" :total="50"></el-pagination>
  </d2-container>
</template>

<script>
import * as swiper from "@/api/shop.swiper";
import * as catalog from "@/api/shop.catalog";

export default {
  name: "swiper",
  components: {},
  data() {
    return {
      skuList: null,
      catalogList: null,
      dialogImageUrl: "",
      fileList: [],
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "100px",
      dialogVisible: false,
      createOrEdit: "",
      listQuery: {
        pageStart: 1,
        pageSize: 25
      }
    };
  },
  mounted() {
    this.getList();
    this.$route.meta.title = "首页轮播图";

    catalog.fetchList(this.listQuery).then(response => {
      if (response.success) this.catalogList = response.data.list;
    });

    swiper.getSkuList(this.listQuery).then(response => {
      if (response.success) this.skuList = response.data.list;
    });

    this.getList();
  },
  methods: {
    onSelectedChange(e) {
      console.log("onSelectedChange", e);
      this.listQuery.categoryId = e;
      swiper.getSkuList(this.listQuery).then(response => {
        if (response.success) this.skuList = response.data.list;
      });
    },

    handleUploadSuccess(response, file, fileList) {
      console.log("handleUploadSuccess", response);
      if (response.success) this.fileList = [];
      this.fileList.push({
        url: response.downloadPath
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
      const index = this.fileList.indexOf(file, 0);
      if (index > -1) {
        this.fileList.splice(index, 1);
      }
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    showCreateDlg(formName) {
      this.dialogFormVisible = true;
      this.createOrEdit = "新建";
      // this.$refs[formName].resetFields();
      this.form = {};
    },

    showEditDlg(row) {
      this.dialogFormVisible = true;
      this.createOrEdit = "编辑";
      this.form = row;
      this.fileList = [{ url: this.form.imgUrl }];
    },

    handleUploadSuccess(response, file, fileList) {
      console.log(response);
      if (response.success) this.downloadPath = response.downloadPath;
    },

    handleCreateNew(formName) {
      let that = this;

      const set = that.$refs;
      set[formName].validate(valid => {
        if (valid) {
          if (that.createOrEdit == "新建") {
            let input = {
              designType: 1,
              imgUrl: that.downloadPath,
              linkType: 1,
              linkUrl: that.form.linkUrl
            };

            swiper.insert(input).then(resp => {
              if (resp.success) {
                that.dialogFormVisible = false;
                that.getList();
                that.$notify({
                  title: "提示",
                  message: "操作成功",
                  type: "success",
                  duration: 2000
                });
              } else {
                that.$notify({
                  title: "提示",
                  message: "操作失败",
                  type: "error",
                  duration: 2000
                });
              }
            });
          }

          if (that.createOrEdit == "编辑") {
            let input = {
              id: that.form.id,
              designType: 1,
              imgUrl: that.downloadPath,
              linkType: 1,
              linkUrl: that.form.linkUrl
            };
            swiper.update(input).then(resp => {
              if (resp.success) {
                that.dialogFormVisible = false;
                that.getList();
                that.$notify({
                  title: "提示",
                  message: "操作成功",
                  type: "success",
                  duration: 2000
                });
              } else {
                that.$notify({
                  title: "提示",
                  message: "操作失败",
                  type: "error",
                  duration: 2000
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    getList() {
      swiper.fetchList().then(response => {
        if (response.success) this.tableData = response.data;
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    handleDelete(row) {
      this.$confirm("确认删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        swiper.deleteById(row.id).then(resp => {
          if (resp.success) {
            this.getList();
            this.$notify({
              title: "提示",
              message: "操作成功",
              type: "success",
              duration: 2000
            });
          } else {
            this.$notify({
              title: "提示",
              message: resp.bizMessage,
              type: "error",
              duration: 2000
            });
          }
        });
      });
    }
  }
};
</script>
